package com.baizhi.controller;


import com.aliyuncs.CommonResponse;
import com.baizhi.dto.PageDTO;
import com.baizhi.entity.Feedback;
import com.baizhi.entity.User;
import com.baizhi.service.FeedbackService;
import com.baizhi.service.UserService;
import com.baizhi.uitl.SendSmsUtil;
import com.baizhi.vo.CommonResult;
import com.baizhi.vo.CommonResultPageVO;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@RestController  //将controller组件对象交给工厂管理
@CrossOrigin  //跨域： 当一个请求的url的协议、域名、端口三者之间任意一个与当前页面url不同即为跨域
@RequestMapping("user")
public class UserController {

    @Resource
    UserService userService;


    //根据页码，大小查询数据
    @PostMapping("queryAllPage")
    public CommonResultPageVO queryAllPage(@RequestBody PageDTO pageDTO){

        System.out.println("pageDTO: "+pageDTO);

        return userService.queryAllPage(pageDTO);
    }


    //更新
    @PostMapping("update")
    public CommonResult update(@RequestBody User user){

        System.out.println("user: "+user);
        try {
            userService.update(user);
            return CommonResult.success("修改成功！！！");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.faild("修改失败！！！");
        }
    }

    //添加
    @PostMapping("add")
    public CommonResult add(@RequestBody User user){

        System.out.println("user: "+user);
        try {
            userService.add(user);
            return CommonResult.success("添加成功！！！");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.faild("添加失败！！！");
        }
    }


    //文件上传
    @PostMapping("uploadAvatarUrl")
    public HashMap<String, String> uploadAvatarUrl(MultipartFile avatarUrl){

        System.out.println("文件大小："+avatarUrl.getSize());
        System.out.println("文件名："+avatarUrl.getOriginalFilename());
        System.out.println("文件类型："+avatarUrl.getContentType());

        String fileName = userService.uploadAvatarUrl(avatarUrl);
        System.out.println("返回的文件名："+fileName);

        HashMap<String, String> map = new HashMap<>();

        map.put("fileName",fileName);

        return map;
    }


    //根据id查询
    @GetMapping("queryById")
    public User queryById(Integer id){
        System.out.println("userId: "+id);
        return userService.queryById(id);
    }


    //删除
    @DeleteMapping("delete")
    public CommonResult delete(User user){
        System.out.println("userId: "+user);
        try {
            userService.delete(user);
            return CommonResult.success("删除成功！！！");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.faild("删除失败！！！");
        }
    }


    //获取手机验证码
    @Resource
    StringRedisTemplate stringRedisTemplate;

    @GetMapping("getPhoneCode")
    public HashMap<String, Object> getPhoneCode(String phone){

        //1.随机数验证码
        String random = SendSmsUtil.getRandom(6);

        //2.使用redis存储随机数
        stringRedisTemplate.opsForValue().set(phone,random,2, TimeUnit.MINUTES);

        //3.发送手机验证码
        String message = SendSmsUtil.sendPhone(phone, random);

        HashMap<String, Object> map = new HashMap<>();
        map.put("message",message);

        if(message.equals("发送成功")){
            map.put("status",200);
        }else{
            map.put("status",500);
        }

        return map;
    }

    //验证手机验证码
    @GetMapping("phoneCheck")
    public void phoneCheck(String phone,String codes){

        //1.从redis中取出验证码
        //2.验证
        //获取redis中String类型的操作
        ValueOperations<String, String> stringOption = stringRedisTemplate.opsForValue();

        //1.获取验证码
        String codeId = stringOption.get("phone");

        //3.返回message  校验结果

    }
}
